package com.neusoft.elm.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.neusoft.elm.dao.BusinessDao;
import com.neusoft.elm.entity.Business;

/**
 * Servlet implementation class BController
 */
// http://127.0.0.1:8080/elm99/b.do?action=getall
// @WebServlet :为servlet定义url路径
@WebServlet("/b.do")
public class BController extends HttpServlet {
	private static final String ACTION_GETALL = "getall";
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public BController() {
		super();
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 解决乱码问题
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		// 封装表单数据
		Business business = null;
		// 识别目的
		String action = request.getParameter("action");
		switch (action) {

		case ACTION_GETALL:
			getBusinessList(request, response, business);
			break;

		default:
			break;
		}

	}

	private void getBusinessList(HttpServletRequest request, HttpServletResponse response, Business business)
			throws ServletException, IOException {
		// 调用模型类代码
		BusinessDao businessDao = new BusinessDao();
		// 接收model类的执行结果
		List<Business> lbs = businessDao.getAll();
		if (lbs != null && lbs.size() > 0) {
			// 调用视图渲染数据
			request.setAttribute("lbs", lbs);
			// 站内转发
			request.getRequestDispatcher("/businesslist.jsp").forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet1(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 解决乱码问题
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");

		String[] ss = { "234", "ased", "rfopj", "idr", "fgh", "jker", "tuio", "wertou", "iprgd", "jkl", "sdxfi" };

		// 实现传递数据
		// 把request --- > Map ： 数据域
		// key --value
		// 为request对象添加一个K-V数据项
		request.setAttribute("info", ss);
		// 修改
		// request.setAttribute("info", ss);
		// 新增
		// request.setAttribute("info1", ss);
		// 移除
		// request.removeAttribute("info1");

		// http://127.0.0.1:8080/elm99/businesslist.jsp
		// path / --- > Web APP的根 http://127.0.0.1:8080/elm99
		// RequestDispatcher :搬运工
		// RequestDispatcher dispatcher =
		// request.getRequestDispatcher("/businesslist.jsp");
		// 实施对象搬运
		// dispatcher.forward(request, response);
		// 实现了请求、响应的站内转发
		request.getRequestDispatcher("/businesslist.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
